![]() |
ubuntu.com - launchpad.net - ubuntu help
|
|
|||||||
|
Recurring Discussions Some discussions seem to come up over and over. This is a place for those sorts of topics. |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Day Old Decaf
![]() Join Date: Feb 2007
Beans: 236
|
How NOT to write a shared library
I'm interested in audio, so I'm looking over the (frankly horrible) documentation for this new "Pulse Audio" thing, and I see this function in its shared lib:
Code:
pa_xmalloc() Allocates the specified number of bytes, just like malloc() does. However, in case of OOM, terminate. Second of all, in the case of "out of memory", it just terminates your app right then and there??? Oh great, The enduser spends an hour working on something, does some operation, and BANG! -- all his hard work goes down the drain because some shared library decides it wants to terminate the app. Absolutely ridiculous. And I see that other pulse audio functions call this pa_xmalloc too, so good luck getting around it. I can just see it now. Every programmer who uses pulse audio will need to add a feature to his app called "Auto-save before every Pulse Audio call because this may be your last chance to preserve your work". Yeah, that should really yield "low latency". Sorry, but anyone who puts a call to exit() in a shared library should simply be shot dead. That's really, really crummy, amateurish programming. In a shared lib, if something fails, the professional thing to do is return some error code/signal to the app, indicating that the function failed. Let the app decide what it wants to do. You don't terminate the app on your own. And anyone who makes such a shared library a part of his operating system should be shot dead, and then stabbed through the heart with a stake just to make sure he's really dead. God help us if this pulse audio thing is ever chosen to replace ALSA in the kernel. This will make Linux stability and reliability go to hell if you have operating system calls terminating apps at will. Last edited by j_g; November 14th, 2007 at 06:46 PM.. |
|
|
|
|
|
#2 |
|
Way Too Much Ubuntu
![]() Join Date: May 2007
Location: Basildon, England
Beans: 270
Ubuntu UNR
|
Re: How NOT to write a shared library
I would like to echo the sentiment of this post.
In the days when I programmed we often had to fit stuff into the small model (64K code and data) and if we were lucky the next one up (64K code, 64K data). That forced you to write efficient code - pointers not indexed arrays etc, functions not inline code etc. Now we have almost limitless memory it has led to some poor programming. Just to add that of course every function should really return with an termination code. One case I remember the programmer was using long jumps on error leaving all sorts of crap behind when he did so, such as not forcing data writes, not unallocating memory. Eh! those were the days!!
__________________
Mick 'n Keef rock, Chas beats time and Ronnie is the new boy Registered as user 466848 with the Linux Counter. Registered Ubuntu User 22858. Our company website Virtualised Karmic and Lucid user simultaneously. Hardy web server. |
|
|
|
|
|
#3 |
|
Way Too Much Ubuntu
![]() Join Date: Oct 2005
Location: UK
Beans: 321
|
Re: How NOT to write a shared library
Every call to malloc should be checked. So rather than put the checking code at every point you call malloc, you put that code in one function normally named 'xmalloc', and call that instead.
|
|
|
|
|
|
#4 |
|
Tall Cafè Ubuntu
![]() Join Date: Oct 2006
Beans: 2,687
Ubuntu 7.10 Gutsy Gibbon
|
Re: How NOT to write a shared library
But you still need to check for an exception in the calling code. That's the entire reason malloc returns NULL when it fails... That's your exception.
__________________
|
|
|
|
|
|
#5 |
|
Way Too Much Ubuntu
![]() |
Re: How NOT to write a shared library
The real kicker here is that you spent 10-20 minutes writing this post. Did you even bother to spend time emailing the author of the library to express your concerns? Probably not. Yeah, everyone should be "shot dead" because how they think something should be done. Give me a break.
__________________
-Skeeterbug |
|
|
|
|
|
#6 | |
|
Day Old Decaf
![]() Join Date: Feb 2007
Beans: 236
|
Re: How NOT to write a shared library
Quote:
longjmp() is a careless approach toward error handling that only a lazy and negligent programmer would use. Code that uses longjmp is historically buggy and unstable. If someone is using longjmp, he seriously needs to rethink his design. <snip> Last edited by bapoumba; November 14th, 2007 at 02:12 PM.. Reason: snipped out inappropriate comment. |
|
|
|
|
|
|
#7 | |
|
Day Old Decaf
![]() Join Date: Feb 2007
Beans: 236
|
Re: How NOT to write a shared library
Quote:
Sorry, but I do not find this technique to be acceptable in terms of reliability, stability, nor even usability. I would never use software that could, at any moment, dump the hard work I had done, or make running apps just disappear at random in a puff of smoke. Please rethink your error handling techniques, for the sake of endusers everywhere. |
|
|
|
|
|
|
#8 |
|
Day Old Decaf
![]() Join Date: Feb 2007
Beans: 236
|
Re: How NOT to write a shared library
If the pulse audio author actually had his software peer-reviewed before it was put into Fedora, and nobody had balked at this, then I have no faith whatsoever in his peer-review process. A shared lib doesn't need to terminate an app that uses it. ALSA doesn't do it, and there's lots of malloc calls there. Calling exit() in a shared lib is simply sloppy, negligent error handling. No experienced programmer capable of writing quality code should need to be told this. He should already know it.
|
|
|
|
|
|
#9 | |
|
Way Too Much Ubuntu
![]() Join Date: Oct 2005
Location: UK
Beans: 321
|
Re: How NOT to write a shared library
Quote:
Edit: After reading the code (xmalloc.c) the author does write to stderr. Last edited by public_void; November 14th, 2007 at 04:37 PM.. |
|
|
|
|
|
|
#10 | |
|
Tall Cafè Ubuntu
![]() Join Date: Oct 2006
Beans: 2,687
Ubuntu 7.10 Gutsy Gibbon
|
Re: How NOT to write a shared library
Quote:
__________________
|
|
|
|
|
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|